<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增栏目</title>
    <link rel="stylesheet" href="/static/pengpai/component/pear/css/pear.css" />
    <link href="/static/pengpai/component/layui/layui.js" rel="stylesheet">
</head>
<body>
<form class="layui-form layui-col-xs6 layui-col-md4 layui-col-md-offset4" action="" onsubmit="return false">
    <div class="layui-form-item"></div>
    <div class="layui-form-item">
        <label class="layui-form-label">上级栏目:</label>
        <div class="layui-input-block">
            <ul class="dtree" data-id="-1" id="selectParent" name="parentId"></ul>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">栏目名称:</label>
        <div class="layui-input-block">
            <input type="text" name="categoryName" required  lay-verify="required" placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">栏目类型:</label>
        <div class="layui-input-block">
            <select name="categoryModel" lay-verify="required" lay-search>
                <option value=""></option>
                <option value="1">封面</option>
                <option value="2">列表</option>
                <option value="3">外部链接</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">访问地址:</label>
        <div class="layui-input-block">
            <input type="text" name="visitUrl"  placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">封面模板:</label>
        <div class="layui-input-block">
            <input type="text" name="coverTemp"  placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">列表模板:</label>
        <div class="layui-input-block">
            <input type="text" name="listTemp"  placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章模板:</label>
        <div class="layui-input-block">
            <input type="text" name="articleTemp"  placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">外部链接:</label>
        <div class="layui-input-block">
            <input type="text" name="linkUrl"  placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">跳转方式:</label>
        <div class="layui-input-block">
            <select name="linkTarget" lay-search>
                <option value=""></option>
                <option value="1">当前页（_self）</option>
                <option value="2">新标签页（_blank）</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">栏目介绍:</label>
        <div class="layui-input-block">
            <textarea name="categoryDescription" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">栏目内容:</label>
        <div class="layui-input-block">
            <textarea name="content" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">显示顺序:</label>
        <div class="layui-input-block">
            <input type="text" name="sort"  placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上传图片:</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="test1">上传图片</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1" style="height: 150px; width: 150px">
                <p id="demoText"></p>
                <input id="resData" type="hidden" name="fileCode"  placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
            </div>
            <div style="width: 95px;">
                <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
                    <div class="layui-progress-bar" lay-percent=""></div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="categoryAdd">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="/static/pengpai/component/layui/layui.js"></script>
<script src="/static/pengpai/component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'dtree', 'popup'], function() {
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        let popup = layui.popup;
        var upload = layui.upload;
        var layer = layui.layer;
        var element = layui.element;

        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#test1',
            url: '/sysFileinfo/upload', //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });

                element.progress('demo', '0%'); //进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function(res){
                //如果上传失败
                if(res.code == 500){
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                //……
                $('#demoText').html(''); //置空上传失败的状态
                // var demoText = $('#demoText');
                // demoText.html('<input type="text" name="imagePath" th:value="#{res.code}" placeholder="请输入输入框内容" autocomplete="off" class="layui-input">');
                const resData = document.getElementById('resData');
                resData.value = res.data
            },
            error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            },
            //进度条
            progress: function(n, elem, e){
                element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });

        dtree.renderSelect({
            elem: "#selectParent",
            url: "/webCategory/selectParent",
            method: 'get',
            selectInputName: {nodeId: "parentId", context: "categoryName"},
            skin: "layui",
            dataFormat: "list",
            response: {treeId: "categoryId", parentId: "parentId", title: "categoryName"},  //修改response中返回数据的定义
            selectInitVal: "0"
        });

        form.on('submit(categoryAdd)', function(data) {
            $.post("/webCategory/create",data.field,function(r){
                console.info(r.code);
                if(r.code === 200){
                    popup.success(r.msg, function () {
                        // console.log(obj);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        parent.location.reload();
                    });
                }else{
                    popup.failure(r.msg);
                }
            });
        });
    })
</script>
<script>
</script>
</body>
</html>
